package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import com.qy.dao.OrderDriverDao;

import java.util.List;
import java.util.Map;
import java.sql.Types;

import com.qy.db.DbParameter;

import java.util.HashMap;
import java.util.ArrayList;

import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.OrderDriver;
import com.qy.db.BaseDao;

/**
* u_order_driver daoImpl实现类的生成
* @author zhangwenlong
* @date Wed Feb 07 11:28:07 GMT+08:00 2018
*/ 
public class OrderDriverDaoImpl extends BaseDao  implements OrderDriverDao {

	@Override
	public boolean createOrderDriver(OrderDriver orderDriver) {
		boolean result = false;
		int id = -1;
		if (orderDriver.beginAdd()) {
			String sql = "insert into u_order_driver (id,order_id,start_time,end_time,f_image,s_image,t_image,fo_image,fi_image,si_image,status,driver_id,evaluate,evaluate_star,cancel_reason,count_down_time,service_charge) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, orderDriver.getId()));
			param.put(2, new DbParameter(Types.INTEGER, orderDriver.getOrderId()));
			param.put(3, new DbParameter(Types.TIMESTAMP, orderDriver.getStartTime()));
			param.put(4, new DbParameter(Types.TIMESTAMP, orderDriver.getEndTime()));
			param.put(5, new DbParameter(Types.VARCHAR, orderDriver.getFImage()));
			param.put(6, new DbParameter(Types.VARCHAR, orderDriver.getSImage()));
			param.put(7, new DbParameter(Types.VARCHAR, orderDriver.getTImage()));
			param.put(8, new DbParameter(Types.VARCHAR, orderDriver.getFoImage()));
			param.put(9, new DbParameter(Types.VARCHAR, orderDriver.getFiImage()));
			param.put(10, new DbParameter(Types.VARCHAR, orderDriver.getSiImage()));
			param.put(11, new DbParameter(Types.INTEGER, orderDriver.getStatus()));
			param.put(12, new DbParameter(Types.INTEGER, orderDriver.getDriverId()));
			param.put(13, new DbParameter(Types.VARCHAR, orderDriver.getEvaluate()));
			param.put(14, new DbParameter(Types.INTEGER, orderDriver.getEvaluateStar()));
			param.put(15, new DbParameter(Types.VARCHAR, orderDriver.getCancelReason()));
			param.put(16, new DbParameter(Types.TIMESTAMP, orderDriver.getCountDownTime()));
			param.put(17, new DbParameter(Types.INTEGER, orderDriver.getServiceCharge()));
			id = execLastId(sql, param);
			result = id > -1;
			orderDriver.commitAdd(result);
			orderDriver.setId(id);
		}
		return result;
	}

	@Override
	public List<OrderDriver> getOrderDriverList() {
		String sql = "select * from u_order_driver;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<OrderDriver> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<OrderDriver>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public OrderDriver getOrderDriverById(int id) {
		String sql = "select * from u_order_driver where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		OrderDriver orderDriver = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					orderDriver = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return orderDriver;
	}

	@Override
	public boolean updateOrderDriver(OrderDriver orderDriver) {
		boolean result = false;
		if (orderDriver.beginUpdate()) {
			String sql = "update u_order_driver set order_id=?,start_time=?,end_time=?,f_image=?,s_image=?,t_image=?,fo_image=?,fi_image=?,si_image=?,status=?,driver_id=?,evaluate=?,evaluate_star=?,cancel_reason=?,count_down_time=?,service_charge=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, orderDriver.getOrderId()));
			param.put(2, new DbParameter(Types.TIMESTAMP, orderDriver.getStartTime()));
			param.put(3, new DbParameter(Types.TIMESTAMP, orderDriver.getEndTime()));
			param.put(4, new DbParameter(Types.VARCHAR, orderDriver.getFImage()));
			param.put(5, new DbParameter(Types.VARCHAR, orderDriver.getSImage()));
			param.put(6, new DbParameter(Types.VARCHAR, orderDriver.getTImage()));
			param.put(7, new DbParameter(Types.VARCHAR, orderDriver.getFoImage()));
			param.put(8, new DbParameter(Types.VARCHAR, orderDriver.getFiImage()));
			param.put(9, new DbParameter(Types.VARCHAR, orderDriver.getSiImage()));
			param.put(10, new DbParameter(Types.INTEGER, orderDriver.getStatus()));
			param.put(11, new DbParameter(Types.INTEGER, orderDriver.getDriverId()));
			param.put(12, new DbParameter(Types.VARCHAR, orderDriver.getEvaluate()));
			param.put(13, new DbParameter(Types.INTEGER, orderDriver.getEvaluateStar()));
			param.put(14, new DbParameter(Types.VARCHAR, orderDriver.getCancelReason()));
			param.put(15, new DbParameter(Types.TIMESTAMP, orderDriver.getCountDownTime()));
			param.put(16, new DbParameter(Types.INTEGER, orderDriver.getServiceCharge()));
			param.put(17, new DbParameter(Types.INTEGER, orderDriver.getId()));
			result = execNoneQuery(sql, param) > -1;
			orderDriver.commitUpdate(result);
		}
		return result;
	}

	public OrderDriver resultTo(ResultSet rs) throws SQLException {
		OrderDriver orderDriver = new OrderDriver();
		orderDriver.setId(rs.getInt("id"));
		orderDriver.setOrderId(rs.getInt("order_id"));
		orderDriver.setStartTime(rs.getTimestamp("start_time"));
		orderDriver.setEndTime(rs.getTimestamp("end_time"));
		orderDriver.setFImage(rs.getString("f_image"));
		orderDriver.setSImage(rs.getString("s_image"));
		orderDriver.setTImage(rs.getString("t_image"));
		orderDriver.setFoImage(rs.getString("fo_image"));
		orderDriver.setFiImage(rs.getString("fi_image"));
		orderDriver.setSiImage(rs.getString("si_image"));
		orderDriver.setStatus(rs.getInt("status"));
		orderDriver.setDriverId(rs.getInt("driver_id"));
		orderDriver.setEvaluate(rs.getString("evaluate"));
		orderDriver.setEvaluateStar(rs.getInt("evaluate_star"));
		orderDriver.setCancelReason(rs.getString("cancel_reason"));
		orderDriver.setCountDownTime(rs.getTimestamp("count_down_time"));
		orderDriver.setServiceCharge(rs.getInt("service_charge"));
		orderDriver.setOp(Option.None);
		return orderDriver;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}
	
	@Override
	public List<OrderDriver> getOrderDriverList(int driverId, int status) {
	    String sql = "select * from u_order_driver where driver_id=? AND status=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, driverId));
		param.put(2, new DbParameter(Types.INTEGER, status));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		List<OrderDriver> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<OrderDriver>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}


}

